package com.hctforgreen.greenservice.ui.widget;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.hctforgreen.greenservice.R;
import com.hctforgreen.greenservice.TechnicalDataActivityV4;
import com.hctforgreen.greenservice.ctr.HctController;
import com.hctforgreen.greenservice.model.SerieListEntity.ChildSerieEntity;
import com.hctforgreen.greenservice.model.SerieListEntity.SerieEntity;
import com.hctforgreen.greenservice.ui.adapter.ChildSeriesLstAdapter;
import com.hctforgreen.greenservice.utils.HctConstants;
import com.hctforgreen.greenservice.utils.HctResult;

public class ChildSeriesListPopupView extends PopupWindow{
	private View mConvertView;
	private Activity mActivity;
	
	private SerieEntity mCurrentFirstEntity;
	private ChildSerieEntity mCurrentSecondEntity;
	private TechnicalDataActivityV4 mSelf;
	
	public ChildSeriesListPopupView(Activity activity, SerieEntity currentFirstEntity
			, ChildSerieEntity currentSecondEntity, TechnicalDataActivityV4 self){
		super(activity);
		mActivity = activity;
		
		mCurrentFirstEntity = currentFirstEntity;
		mCurrentSecondEntity = currentSecondEntity;
		mSelf = self;
		initWindow();
	}
	private void initWindow(){
		mConvertView = getLayout();

		this.setContentView(mConvertView);
		this.setBackgroundDrawable(new BitmapDrawable());
		this.setOutsideTouchable(true);
		this.setFocusable(true);
		this.setWidth(LayoutParams.FILL_PARENT);
		this.setHeight(LayoutParams.WRAP_CONTENT);
		
		loadDataLst();
	}
	private View getLayout() {
		View layout = mActivity.getLayoutInflater().inflate(
				R.layout.popup_series_list, null);
		return layout;
	}
	
	/*private void loadDataLst(){
		final LinearLayout loadLyt = (LinearLayout) mConvertView
				.findViewById(R.id.lyt_default_list_load);
		final LinearLayout reloadLyt = (LinearLayout) mConvertView
				.findViewById(R.id.lyt_default_list_reload);
		final ListView secondLv = (ListView) mConvertView
				.findViewById(R.id.lst_default_list);

		final Button reloadBtn = (Button) mConvertView
				.findViewById(R.id.btn_default_list_reload);
		
		loadLyt.setVisibility(View.GONE);
		reloadLyt.setVisibility(View.GONE);
		secondLv.setVisibility(View.VISIBLE);
		renderDataLst(secondLv, mCurrentFirstEntity);
	}*/
	private void loadDataLst(){
		
		final LinearLayout loadLyt = (LinearLayout) mConvertView
				.findViewById(R.id.lyt_default_list_load);
		final LinearLayout reloadLyt = (LinearLayout) mConvertView
				.findViewById(R.id.lyt_default_list_reload);
		final ListView firstLv = (ListView) mConvertView
				.findViewById(R.id.lst_default_list);

		final Button reloadBtn = (Button) mConvertView
				.findViewById(R.id.btn_default_list_reload);
		final Handler handler = new Handler() {
			public void handleMessage(Message msg) {

				HctResult hctResult = new HctResult();
				switch (msg.what) {
				case HctConstants.STATUS_SUCCESS:

					loadLyt.setVisibility(View.GONE);
					reloadLyt.setVisibility(View.GONE);
					firstLv.setVisibility(View.VISIBLE);
					
					hctResult = (HctResult) msg.obj;

					SerieEntity sEntity= (SerieEntity) hctResult.data;
					renderDataLst(firstLv, sEntity);

					break;

				case HctConstants.STATUS_FAILD:
					loadLyt.setVisibility(View.GONE);
					reloadLyt.setVisibility(View.VISIBLE);
					firstLv.setVisibility(View.GONE);
					reloadBtn.setOnClickListener(new OnClickListener() {

						@Override
						public void onClick(View v) {
							loadDataLst();
						}
					});
					break;
				}
			}

		};
		if(mCurrentFirstEntity == null){
			loadLyt.setVisibility(View.GONE);
			return;
		}
		loadLyt.setVisibility(View.VISIBLE);
		reloadLyt.setVisibility(View.GONE);
		new Thread() {
			public void run() {
				Message message = new Message();
				try {
					HctController controller = new HctController(
							mActivity);

					HctResult result = controller.getDbSingleSeriesWithDbChildSeries(mCurrentFirstEntity.id);
					if (result.status == HctConstants.STATUS_SUCCESS) {
						message.what = result.status;
						message.obj = result;
					} else {
						message.what = HctConstants.STATUS_FAILD;
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
				handler.sendMessage(message);
			}
		}.start();
	}
	private void renderDataLst(ListView secondLv, 
			SerieEntity serieEntity) {
		if(serieEntity == null){
			return;
		}
		if(serieEntity.childList == null){
			return;
		}
		
		new ChildSeriesLstAdapter(secondLv, mActivity, serieEntity, mCurrentSecondEntity, ChildSeriesListPopupView.this);

	}
	
	
	public void updateWindow(ChildSerieEntity currentSecondEntity){
		mSelf.initChildSerieEntity(currentSecondEntity);
		mSelf.initChildSeriesBtn(currentSecondEntity);
		mSelf.setFocusable(true);
		mSelf.initBooksList();
	}
}
